const local_token = sessionStorage.user_token;

const user_info = JSON.parse(sessionStorage.getItem('user_info'));

const login_path = sessionStorage.getItem('login_path');

/**
 * 解析本地缓存 json 数据
 * @param {string} key
 */
function parseLocalJsonData(key) {
	let value = null;
	const str_value = sessionStorage.getItem(key);
	if (typeof str_value === 'string' && str_value) {
		try {
			value = JSON.parse(str_value);
		} catch (e) {
			console.error(e.message);
			value = null;
		}
	}
	return value;
}

/**
 *  读取本地字符数据
 */
function readLocalStringData(key) {
	return sessionStorage.getItem(key);
}

export default {

	namespaced: true,

	state: {
		login_path,
		user_token: local_token,
		user_info,
		user_roles: parseLocalJsonData('user_roles') || [], // 用户角色
		user_type: readLocalStringData('user_type'), 		// 用户类型
		provider_type: readLocalStringData('provider_type') || '', 	// 代理商类型
		youzan_authorize: readLocalStringData('youzan_authorize') || 0, // 是否有赞授权
	},

	mutations: {
		/* eslint-disable no-param-reassign */
		setToken(state, token) {
			state.user_token = token;
			sessionStorage.setItem('user_token', token);
		},

		setUserInfo(state, info) {
			state.user_info = info;

			sessionStorage.setItem('user_info', JSON.stringify(info));
		},

		deleteToken(state) {
			state.user_token = null;
			sessionStorage.removeItem('user_token');
		},

		setUserRoles(state, user_roles) {
			state.user_roles = user_roles;
			sessionStorage.setItem('user_roles', JSON.stringify(user_roles));
		},

		setUserType(state, user_type) {
			state.user_type = user_type;
			sessionStorage.setItem('user_type', JSON.stringify(user_type));
		},

		// 补充
		setProviderType(state, provider_type) { // 代理商类型
			state.provider_type = provider_type;
			sessionStorage.setItem('provider_type', provider_type);
		},
		setYouzanAuthorize(state, authorize) { // 有赞授权
			state.youzan_authorize = authorize;

			sessionStorage.setItem('youzan_authorize', authorize);
		},

		/**
		 * 使用用户信息表单字段更新原有信息
		 * @param {*} state
		 * @param {*} form
		 */
		updateUserInfo(state, form) {
			({
				sex: state.user_info.sex,
				phone: state.user_info.phone,
				name: state.user_info.name,
				email: state.user_info.email,
				born_at: state.user_info.born_at,
				account: state.user_info.account,
			} = form);
			sessionStorage.setItem('user_info', JSON.stringify(state.user_info));
		},
		setLoginPath(state, path) {
			state.login_path = path;
			sessionStorage.setItem('login_path', path);
		},
		/* eslint-enable no-param-reassign */

	},

	actions: {
		toSetToken(context, token) {
			context.commit('setToken', token);
		},
		toSetUserInfo(context, info) {
			context.commit('setUserInfo', info);
		},
		toSetUserRoles(context, roles) {
			context.commit('setUserRoles', roles);
		},
		toSetUserType(context, user_type) {
			context.commit('setUserType', user_type);
		},
		toSetLoginPath(context, path) {
			context.commit('setLoginPath', path);
		},
		// 补充
		toSetProviderType(context, provider_type) {
			context.commit('setProviderType', provider_type);
		},
		toSetYouzanAuthorize(context, youzan_authorize) {
			context.commit('setYouzanAuthorize', youzan_authorize);
		},
	},

	getters: {
		getToken(state) {
			return state.user_token;
		},
		getUserInfo(state) {
			return state.user_info || {};
		},
		getUserRoles(state) {
			return state.user_roles;
		},
		getUserType(state) {
			return state.user_type;
		},
		getLoginPath(state) {
			return state.login_path;
		},
		getUserId(state) {
			return state.user_info && state.user_info.id ? state.user_info.id : null;
		},
		// 补充
		getProviderType(state) { // 代理商类型
			const { provider_type } = state.user_info;
			return provider_type ? parseInt(provider_type, 10) : '';
		},
		getYouzanAuthorize(state) { // 有赞授权
			return parseInt(state.youzan_authorize, 10);
		},
		isVirtualProvider(state) { // 是否是虚拟供应商
			const { provider_type } = state.user_info;
			return provider_type && parseInt(provider_type, 10) === 1;
		},
	},
};
